package com.ruoyi.system.controller;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.system.api.domain.DynamicSql;
import com.ruoyi.system.service.IDynamicSqlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/dynamicsql")
public class SysDynamicSqlController extends BaseController {
    @Autowired
    private IDynamicSqlService dynamicSqlService;

    /**
     * 执行更新sql
     * @param dynamicSql  动态sql
     * @return
     */
    @PostMapping("/doupdate")
    public R<Integer> doUpdate(@RequestBody DynamicSql dynamicSql) {
        try{
            int rowNum = dynamicSqlService.executeSql(dynamicSql.getSql());
            log.info("执行更新sql：{}，返回行数：{}",dynamicSql.getSql(),rowNum);
            return R.ok(rowNum);
        }catch(Exception e){
            log.error("执行更新sql失败！,sql:{}",e,dynamicSql.getSql());
            return R.fail(-1);
        }
    }
}
